package com.itboye.lottery.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.lottery.R;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.BetZqDetailBean;
import com.itboye.lottery.utils.Const;
import com.itboye.lottery.utils.OnItemClickListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created by  HONGDA on 2018/6/19.
 */
public class BetDetailListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity context;
    public List<BetZqDetailBean.NoteRecordEntity> mData = new ArrayList<>();
    private LayoutInflater inflater;
    private String origin_num;
    private String type;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    OnItemClickListener onItemClickListener;

    public BetDetailListAdapter(Activity context, List<BetZqDetailBean.NoteRecordEntity> mData) {
        this.context = context;
        this.mData = mData;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_bet_detail, parent, false);
        AddressListViewHolder holder = new AddressListViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        AddressListViewHolder viewHolder = (AddressListViewHolder) holder;
        if (mData != null) {
            BetZqDetailBean.NoteRecordEntity noteRecordEntity = mData.get(position);
            String content[];
            String rqStyle = "";
            if (noteRecordEntity.getSai() != null) {
                if (!TextUtils.isEmpty(noteRecordEntity.getSai().getRq())) {
                    if (Double.parseDouble(noteRecordEntity.getSai().getRq()) < 0) {
                        rqStyle = "<font color='blue'>(" + noteRecordEntity.getSai().getRq() + ")</font>";
                    } else {
                        rqStyle = "<font color='black'>(" + noteRecordEntity.getSai().getRq() + ")</font>";
                    }
                }
            }
            if (type.equals("2")) {
                //篮球
                content = getLqBetContentAndPrizeContent(noteRecordEntity);
                viewHolder.tv1.setText(Html.fromHtml(noteRecordEntity.getQiudui().getNum() + "<br />" + noteRecordEntity.getQiudui().getA_cn() + " VS " + noteRecordEntity.getQiudui().getH_cn() + rqStyle));
            } else {//足球
                content = getBetContentAndPrizeContent(noteRecordEntity);
                viewHolder.tv1.setText(Html.fromHtml(noteRecordEntity.getQiudui().getNum() + "<br />" + noteRecordEntity.getQiudui().getH_cn() + " VS " + noteRecordEntity.getQiudui().getA_cn() + rqStyle));
            }



            viewHolder.tv2.setText(Html.fromHtml(content[1]));
            viewHolder.tv3.setText(Html.fromHtml(content[0]));
        }
    }

    /*篮球投注表格绘制*/
    private String[] getLqBetContentAndPrizeContent(BetZqDetailBean.NoteRecordEntity ballInfoEntity) {
        String[] values = new String[]{"", "", "", ""};
        JSONObject jsonObject = null;
        if (ballInfoEntity.getQiudui() == null || "null".equals(ballInfoEntity.getQiudui())) {
            ByAlert.alert("球队信息有误");
            return null;
        }
        String bDate = ballInfoEntity.getQiudui().getB_date().replaceAll("-", "");
        values[2] = bDate;
        String sf = "", sf_tag = "", rsf = "", rsfTag = "", dxf = "", dxf_tag = "", sfc = "", sfc_tag = "";
        if (ballInfoEntity.getSai() != null) {
            sf = ballInfoEntity.getSai().getSpf(false);//非让胜负
            sf_tag = ballInfoEntity.getSai().getSpf(false);
            rsf = ballInfoEntity.getSai().getR(false);//让分胜负
            rsfTag = ballInfoEntity.getSai().getR(false);
            dxf = ballInfoEntity.getSai().getTot();//大小分
            dxf_tag = ballInfoEntity.getSai().getTot();
            sfc = ballInfoEntity.getSai().getSfc();//胜负差
            sfc_tag = ballInfoEntity.getSai().getSfc();
        }
        try {
            if (!TextUtils.isEmpty(origin_num)) {
                jsonObject = new JSONObject(origin_num);
                String num = ballInfoEntity.getQiudui().getNum_type();
                JSONObject jsonObject1 = jsonObject.getJSONObject(bDate + num);
                Iterator<String> iterator = jsonObject1.keys();
                HashMap<String, String> hashMap = new HashMap<>();
                while (iterator.hasNext()) {
                    StringBuffer stringBufferValue = new StringBuffer();
                    StringBuffer kaijiang = new StringBuffer();
                    String key = iterator.next();
                    hashMap.put(key, key);
                    JSONObject jsonObjectValue = jsonObject1.getJSONObject(key);
                    Iterator<String> iterator2 = jsonObjectValue.keys();
                    /**
                     * 胜负 3001
                     */
                    /**
                     * 让球胜负 3002
                     */
                    /**
                     * 胜分差 3003
                     */
                    /**
                     * 猜大小分 大于n值|小于n值 3004
                     */
                    switch (key) {
                        case "3001"://胜负
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                String sfValue = "";
                                if (key2.equals("0")) {
                                    sfValue = "客胜";
                                }
                                if (key2.equals("3")) {
                                    sfValue = "主胜";
                                }
                                if (sf_tag.equals(key2) && !TextUtils.isEmpty(sfValue)) {
                                    sf = Const.Companion.getHtmlHead() + sfValue + "(" + value + ")<br />" + Const.Companion.getHtmlFoot();
                                    stringBufferValue.append(Const.Companion.getHtmlHead() + sfValue + "(" + value + ")<br />" + Const.Companion.getHtmlFoot());
                                } else if (key2.equals("0") || (key2.equals("3"))) {
                                    stringBufferValue.append(sfValue + "(" + value + ")<br />");
                                }
//                                if (!TextUtils.isEmpty(sfValue)) {
//
//                                }
                            }
                            break;
                        case "3002"://让胜负
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                String rsfValue = "";
                                if (key2.equals("0")) {
                                    rsfValue = "让客胜";
                                }
                                if (key2.equals("3")) {
                                    rsfValue = "让主胜";
                                }
                                if (rsfTag.equals(key2) && !TextUtils.isEmpty(rsfValue)) {
                                    rsf = Const.Companion.getHtmlHead() + rsfValue + "(" + value + ")<br />" + Const.Companion.getHtmlFoot();
                                    stringBufferValue.append(Const.Companion.getHtmlHead() + rsfValue + "(" + value + ")" + "<br />" + Const.Companion.getHtmlFoot());
                                } else if (key2.equals("0") || (key2.equals("3"))) {
                                    stringBufferValue.append(rsfValue + "(" + value + ")" + "<br />");
                                }
                            }
                            break;
                        case "3003": //胜负差
                            boolean hasAdLHead = false, hasAdWHead = false;
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                String sfcValue = "";
                                if (key2.startsWith("l")) {
                                    if (!hasAdLHead) {
                                        hasAdLHead = true;
                                        sfcValue = "客胜";
                                        stringBufferValue.append("客胜");
                                    }
                                    if (key2.equals("l1")) {
                                        sfcValue = "[1-5]";
                                    }
                                    if (key2.equals("l2")) {
                                        sfcValue = "[6-10]";
                                    }
                                    if (key2.equals("l3")) {
                                        sfcValue = "[11-15]";
                                    }
                                    if (key2.equals("l4")) {
                                        sfcValue = "[16-20]";
                                    }
                                    if (key2.equals("l5")) {
                                        sfcValue = "[21-25]";
                                    }
                                    if (key2.equals("l6")) {
                                        sfcValue = "[26+]";
                                    }
                                } else if (key2.startsWith("w")) {
                                    if (!hasAdWHead) {
                                        hasAdWHead = true;
                                        stringBufferValue.append("主胜");
                                        sfcValue = "主胜";
                                    }
                                    if (key2.equals("w1")) {
                                        sfcValue = "[1-5]";
                                    }
                                    if (key2.equals("w2")) {
                                        sfcValue = "[6-10]";
                                    }
                                    if (key2.equals("w3")) {

                                        sfcValue = "[11-15]";
                                    }
                                    if (key2.equals("w4")) {

                                        sfcValue = "[16-20]";
                                    }
                                    if (key2.equals("w5")) {
                                        sfcValue = "[21-25]";
                                    }
                                    if (key2.equals("w6")) {
                                        sfcValue = "[26+]";
                                    }
                                }

                                if (sfc_tag.equals(key2) && !TextUtils.isEmpty(sfcValue)) {
                                    sfc = Const.Companion.getHtmlHead() + (hasAdWHead ? "主胜" : "客胜") + sfcValue + "(" + value + ")<br />" + Const.Companion.getHtmlFoot();
                                    stringBufferValue.append(Const.Companion.getHtmlHead() + sfcValue.replace(",", "") + "(" + value + ")" + "<br />" + Const.Companion.getHtmlFoot());
                                } else if (key2.startsWith("l") || (key2.startsWith("w"))) {
                                    stringBufferValue.append(sfcValue + "(" + value + ")<br />");
                                }
//                                if (!TextUtils.isEmpty(sfcValue)) {
//                                    stringBufferValue.append(",");
//                                }
                            }
                            break;
                        case "3004":
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                String dxfValue = "";
                                if (key2.equals("0")) {
                                    dxfValue = "小分";
                                }
                                if (key2.equals("1")) {
                                    dxfValue = "大分";
                                }

                                if (dxf_tag.equals(key2) && !TextUtils.isEmpty(dxfValue)) {
                                    dxf = Const.Companion.getHtmlHead() + dxfValue.replace(",", "") + "(" + value + ")<br />" + Const.Companion.getHtmlFoot();
                                    stringBufferValue.append(Const.Companion.getHtmlHead() + dxfValue + "(" + value + ")<br />" + Const.Companion.getHtmlFoot());
                                } else if (key2.equals("0") || (key2.equals("1"))) {
                                    stringBufferValue.append(dxfValue + "(" + value + ")<br />");
                                }
                                //预设总分
                                if (key2.equals("score")) {
                                    stringBufferValue.append("预设分(" + value + ")<br />");
                                }
                            }
                            break;
                    }
                    values[0] += " " + (stringBufferValue.toString().endsWith(",") ? (stringBufferValue.toString().substring(0, stringBufferValue.length() - 1)) : stringBufferValue.toString());
                    values[3] = key;
                }
            } else {
                values[0] = ("");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        values[0] = (values[0].endsWith(",") ? (values[0].substring(0, values[0].length() - 1)) : values[0]);
        if (ballInfoEntity.getSai() == null || "null".equals(ballInfoEntity.getSai())) {

        } else {
            values[1] = ballInfoEntity.getSai().getSf(sf)
                    + ballInfoEntity.getSai().getRsf(rsf)
                    + ballInfoEntity.getSai().getDxf(dxf)
                    + ballInfoEntity.getSai().getSfc(sfc);
        }

        return values;
    }

    /*足球投注表格绘制*/
    private String[] getBetContentAndPrizeContent(BetZqDetailBean.NoteRecordEntity ballInfoEntity) {
        String[] values = new String[]{"", "", "", ""};
        JSONObject jsonObject = null;
        String bDate = ballInfoEntity.getQiudui().getB_date().replaceAll("-", "");
        values[2] = bDate;
        String spf = "", spf_tag = "", rspf = "", bqc = "", bqc_tag = "", zjq = "", zjq_tag = "", bf = "", bf_tag = "";
        if (ballInfoEntity.getSai() != null) {
            spf = ballInfoEntity.getSai().getSpf(true) + "<br />";//胜平负
            spf_tag = ballInfoEntity.getSai().getSpf(true);
            rspf = "让球胜平负：" + ballInfoEntity.getSai().getR(true);//让胜平负
            bqc = ballInfoEntity.getSai().getBanquan() + "<br />";//半全场
            bqc_tag = ballInfoEntity.getSai().getBanquan();
            zjq = ballInfoEntity.getSai().getTot() + "球<br />";//总进球
            zjq_tag = ballInfoEntity.getSai().getTot();
            bf = ballInfoEntity.getSai().getBf();//比分
            bf_tag = bf;
        }

        try {
            if (!TextUtils.isEmpty(origin_num)) {
                jsonObject = new JSONObject(origin_num);
                String num = ballInfoEntity.getQiudui().getNum_type();
                JSONObject jsonObject1 = jsonObject.getJSONObject(bDate + num);
                Iterator<String> iterator = jsonObject1.keys();
                HashMap<String, String> hashMap = new HashMap<>();
                String peilv = "";
                while (iterator.hasNext()) {
                    StringBuffer stringBufferValue = new StringBuffer();
                    String key = iterator.next();
                    hashMap.put(key, key);
                    JSONObject jsonObjectValue = jsonObject1.getJSONObject(key);
                    Iterator<String> iterator2 = jsonObjectValue.keys();
                    // 3010: 胜平负
                    // 3006:让球胜平负
                    // 3007: 全场比分(90分钟)
                    // 3008: 进球数
                    // 3009: 半全场胜负  这个暂时不支持
                    // 3011: 混合过关 （一注投注中包含2个玩法以上的就是混合过关）
                    switch (key) {
                        case "3010":
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                String spfValue = "";
                                if (key2.equals("0")) {
                                    spfValue = ("客胜");
                                }
                                if (key2.equals("1")) {
                                    spfValue = ("平");
                                }
                                if (key2.equals("3")) {
                                    spfValue = ("主胜");
                                }
                                if (spf_tag.equals(spfValue) && !TextUtils.isEmpty(spfValue)) {
                                    spf = Const.Companion.getHtmlHead() + spf_tag + "(" + value + ")<br />" + Const.Companion.getHtmlFoot();
                                    stringBufferValue.append(Const.Companion.getHtmlHead() + spfValue + "(" + value + ")" + Const.Companion.getHtmlFoot());
                                } else if (!key2.equals("single")) {
                                    stringBufferValue.append(spfValue + "(" + value + ")");
                                }
                                if (!TextUtils.isEmpty(spfValue)) {
                                    stringBufferValue.append("<br />");
                                }
                            }
                            break;
                        case "3007"://全场比分
                            boolean isPrize = false;
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                if (!key2.equals("single")) {
                                    if (jsonObjectValue.get(key2) instanceof JSONObject) {
                                        JSONObject value = jsonObjectValue.getJSONObject(key2);//种类所带表的赔率
                                        Iterator<String> iterator3 = value.keys();
                                        while (iterator3.hasNext()) {
                                            String key3 = iterator3.next();
                                            int score0 = Integer.parseInt(key3.substring(0, 1));
                                            int score1 = Integer.parseInt(key3.substring(1, 2));
                                            int finalScore0 = 0, finalScore1 = 0;
                                            if (!TextUtils.isEmpty(bf_tag)) {
                                                finalScore0 = Integer.parseInt(bf_tag.split(":")[0]);
                                                finalScore1 = Integer.parseInt(bf_tag.split(":")[1]);
                                            }
                                            peilv = value.getString(key3);
                                            if ((score0 + ":" + score1).equals(bf_tag)) {
                                                stringBufferValue.append(Const.Companion.getHtmlHead() + score0 + ":" + score1 + "(" + peilv + ")" + Const.Companion.getHtmlFoot());
                                                isPrize = true;
                                            } else if (key3.equals("99") || key3.equals("90") || key3.equals("09")) {
                                                if (finalScore0 >= 6 && finalScore1 >= 3 && finalScore0 == finalScore1) {
                                                    stringBufferValue.append(Const.Companion.getHtmlHead() + "平其它" + "(" + peilv + ")");
                                                } else if (key3.equals("99")) {
                                                    stringBufferValue.append("平其它" + "(" + peilv + ")");
                                                }
                                                if (finalScore0 >= 6 && finalScore1 >= 3 && finalScore0 > finalScore1) {
                                                    stringBufferValue.append(Const.Companion.getHtmlHead() + "胜其它" + "(" + peilv + ")");
                                                } else if (key3.equals("90")) {
                                                    stringBufferValue.append("胜其它" + "(" + peilv + ")");
                                                }
                                                if (finalScore0 >= 6 && finalScore1 >= 3 && finalScore0 < finalScore1) {
                                                    stringBufferValue.append(Const.Companion.getHtmlHead() + "负其它" + "(" + peilv + ")");
                                                } else if (key3.equals("09")) {
                                                    stringBufferValue.append("负其它" + "(" + peilv + ")");
                                                }
                                            } else {
                                                stringBufferValue.append(score0 + ":" + score1 + "(" + peilv + ")");
                                            }
                                            stringBufferValue.append("<br />");
                                        }
                                    }

                                }
                            }
                            if (isPrize) {
                                bf = Const.Companion.getHtmlHead() + bf_tag + "(" + peilv + ")<br />" + Const.Companion.getHtmlFoot();
                            }
//                            if (ballInfoEntity.getFinal_score() != null || "null".equals(ballInfoEntity.getFinal_score())) {
//                                kaijiang.append(ballInfoEntity.getFinal_score() + ",");
//                            }
                            break;
                        case "3006": //让球胜平负
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                String rspfValue = "";
                                if (!key2.equals("single")) {
                                    if (key2.equals("0")) {
                                        rspfValue = ("让负");
                                    }
                                    if (key2.equals("1")) {
                                        rspfValue = ("让平");
                                    }
                                    if (key2.equals("3")) {
                                        rspfValue = ("让胜");
                                    }

                                    if (rspf.contains(rspfValue) && !TextUtils.isEmpty(rspfValue)) {
                                        rspf = Const.Companion.getHtmlHead() + rspf + "(" + value + ")<br />" + Const.Companion.getHtmlFoot();
                                        stringBufferValue.append("<font color='red'>" + rspfValue + "(" + value + ")" + "</font>");
                                    } else {
                                        stringBufferValue.append(rspfValue + "(" + value + ")");
                                    }
                                    if (!TextUtils.isEmpty(rspfValue)) {
                                        stringBufferValue.append("<br />");
                                    }
                                }
                            }
                            break;
                        case "3008":
                            //总进球
                            boolean isPrizeZjq = false;
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                if (key2.equals("goals")) {
                                    try {
                                        JSONObject jsonObject2 = jsonObjectValue.getJSONObject(key2);
                                        Iterator<String> iterator1 = jsonObject2.keys();
                                        while (iterator1.hasNext()) {
                                            String key3 = iterator1.next();
                                            String valueInner= jsonObject2.getString(key3);
                                            if (key3.equals(zjq_tag)) {
                                                peilv =valueInner;
                                                isPrizeZjq = true;
                                                if (key3.equals("7")) {
                                                    key3 = "7+";
                                                }
                                                stringBufferValue.append(Const.Companion.getHtmlHead() + key3 + "球" + "(" + peilv + ")" + Const.Companion.getHtmlFoot());
                                            } else {
                                                if (key3.equals("7")) {
                                                    key3 = "7+";
                                                }
                                                stringBufferValue.append(key3 + "球" + "(" + valueInner + ")");
                                            }
                                            stringBufferValue.append("<br />");
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }

                                }
                            }
                            if (isPrizeZjq) {
                                zjq = Const.Companion.getHtmlHead() + zjq_tag + "球(" + peilv + ")<br />" + Const.Companion.getHtmlFoot();
                            }
                            break;
                        case "3009":
                            //半全场
                            boolean isPrizeBqc = false;
                            while (iterator2.hasNext()) {
                                String key2 = iterator2.next();//投注种类中所代表的种类
                                String value = jsonObjectValue.getString(key2);//种类所带表的赔率
                                if (!key2.equals("single")) {
                                    String zjqValue = key2.replaceAll("3", "胜").replaceAll("1", "平").replaceAll("0", "负");
                                    peilv = value;
                                    if (zjqValue.equals(bqc_tag)) {
                                        isPrizeBqc = true;
                                        stringBufferValue.append(Const.Companion.getHtmlHead() + zjqValue + "(" + peilv + ")" + Const.Companion.getHtmlFoot());
                                    } else {
                                        stringBufferValue.append(zjqValue + "(" + peilv + ")");
                                    }
                                    stringBufferValue.append("<br />");
                                }
                            }
                            if (isPrizeBqc) {
                                bqc = Const.Companion.getHtmlHead() + bqc_tag.replaceAll("3", "胜").replaceAll("1", "平").replaceAll("0", "负") + "(" + peilv + ")<br />" + Const.Companion.getHtmlFoot();
                            }
                            break;
                        case "3011":
                            break;
                    }
                    values[0] += (stringBufferValue.toString());
                    values[3] = key;
                }
            } else {
                values[0] = ("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        values[0] = (values[0].endsWith(",") ? (values[0].substring(0, values[0].length() - 1)) : values[0]);
        values[1] = spf + rspf + bqc + zjq + bf;
        return values;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void setOriginNum(String origin_num) {
        this.origin_num = origin_num;
    }

    public void setType(String type) {
        this.type = type;
    }

    public class AddressListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private LinearLayout item_bet_detail_root;
        TextView tv1, tv2, tv3;

        public AddressListViewHolder(View itemView) {
            super(itemView);
            item_bet_detail_root = itemView.findViewById(R.id.item_bet_detail_root);
            tv1 = (TextView) item_bet_detail_root.getChildAt(0);
            tv2 = (TextView) item_bet_detail_root.getChildAt(1);
            tv3 = (TextView) item_bet_detail_root.getChildAt(2);
        }

        @Override
        public void onClick(View view) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(view, getAdapterPosition());
            }
        }
    }

}
